// lca
// https://soj.turingedu.cn/problem/61300/
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
const int max_size = 5 + 100;
const int maxn = 5 + 2e5;

int n;
vector<int> adj[maxn];
int fa[maxn], dep[maxn], siz[maxn], son[maxn];

void init(int u, int p) {
    fa[u] = p, dep[u] = dep[p] + 1, siz[u] = 1;
    for (auto v : adj[u]) {
        if (v == p) continue;
        init(v, u);
        siz[u] += siz[v];
        if (siz[v] > siz[son[u]]) son[u] = v;
    }
}
int clk;
int dfn[maxn], top[maxn];

void dfs(int u, int t) { //  当前节点，重链顶节点
    dfn[u] = ++clk, top[u] = t;
    if (son[u]) dfs(son[u], t);
    for (auto v : adj[u]) {
        if (v == fa[u] || v == son[u]) continue;
        dfs(v, v);
    }
}

int lca(int u, int v) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v); // keep u deeper
        u = fa[top[u]];
    }
    return dep[u] < dep[v] ? u : v;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    init(1, 0);
    dfs(1, 1);
    int Q;
    cin >> Q;
    while (Q--) {
        int u, v;
        cin >> u >> v;
        printf("%d\n", lca(u, v));
    }
    // rf(i, n) printf("%d ", top[i]);
    // puts("");
}

/*
5
1 2
1 3
3 4
3 5
5
2 1
4 2
4 5
3 2
1 5

*/